var express = require('express');
const Result = require('../lib/Result');
const Token = require('../util/token');
const Manager = require('../db/models/manager/manager');
var router = express.Router();
/* 登录请求 */
router.post("/", function (req, res) {
    Manager.getManagerByUserName(req.body.username, 1).then(row => {
        if (row[0].isPassword(req.body.password)) {
            row[0].updateLastTime(new Date)
            let rule_id = Array()
            Manager.getRule_ids(req.body.username).then(value=>{
                value.forEach(value=>{
                    rule_id.push(value.id)
                })
                res.json(Result.success({ token: Token.getToken(row[0].id, row[0].username, row[0].super, rule_id) }))
            })
        } else {
            res.json(Result.validateFailed(null))
        }
    })
})
module.exports = router;